package com.kingsystem.kingfinancial_backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kingsystem.kingfinancial_backend.entity.FinancialBook;
import com.kingsystem.kingfinancial_backend.mapper.FinancialBookMapper;
import com.kingsystem.kingfinancial_backend.service.impl.FinancialBookServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author leiyue_yao
 * @since 2025-07-11
 */
@Controller
@RequestMapping("/financialBook")
@CrossOrigin
public class FinancialBookController {
    @Autowired
    private FinancialBookMapper fbm;

    @Autowired
    private FinancialBookServiceImpl fbi;

    @PostMapping("/Add")
    @ResponseBody
    public String Add(@RequestBody FinancialBook fb){
        try{
            fbi.save(fb);
            return "success";
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @PostMapping("/getUserFinancial")
    @ResponseBody
    public List<FinancialBook> getUserFinancial(@RequestParam("user_id") String user_id){
        QueryWrapper<FinancialBook> qw =new QueryWrapper<>();
        qw.eq("user_id", user_id);

        List<FinancialBook> li = fbm.selectList(qw);
        return li;
    }

    @PostMapping("/delInfo")
    @ResponseBody
    public String delInfo(@RequestParam("tableKey") String tableKey){
        QueryWrapper<FinancialBook> qw =new QueryWrapper<>();
        qw.eq("tablek_key", tableKey);

        try{
            fbm.delete(qw);
            return "success";
        } catch (Exception e) {
            return e.getMessage();
        }
    }
}
